Ext.define('CDR.controller.TariffReportController', {
    extend: 'Ext.app.Controller',
    views: [
       'CDR.view.report.TariffReportGrid',
       'Ext.ux.grid.column.Component',
       'Ext.ux.grid.column.CTemplate'
    ],
    stores: [
    	  
    ],
    models: [
    	'CDR.model.report.TariffReportTreeEntityModel'
    ],
    onLaunch:function(){
		
	},
    init: function() {
    	
	  	
        this.control({
           '#x-cdr-tariff-report treepanel' : {
	           	itemclick: function(view,record,dom,idx,event){
	           		if(record.data.id == 'root'){
	           			Ext.getCmp('x-cdr-tariff-report-condition').disable(true);
	           		} else {
	           			Ext.getCmp('x-cdr-tariff-report-condition').enable(true);
	           		}
	           	}
           },
           
           '#x-cdr-tariff-report-condition tariff-call-report-grid toolbar button[action=add]' : {
	           	click: function(cmp, e){
	           		cmp.up('gridpanel').showView(e.getXY());
	           	}
           },
           
           '#x-cdr-tariff-report-condition tariff-call-report-grid toolbar button[action=clear]' : {
	           	click: function(cmp, e){
	           		cmp.up('gridpanel').store.removeAll();
	           	}
           },
           
           'tariff-call-report-entity-tree treepanel' : {
	           	itemclick: function(view,record,dom,idx,event){
	           		var nodeAdd = [];
	           		if("Site" === record.data.type || "pbx" === record.data.type){
           				//reset all check box
           				if(!record.data.selectContact){
           					record.set('selectContact', true);
           					nodeAdd.push(record);
           					view.up('treepanel').store.getRootNode().cascadeBy(
							    function(node){
							    	if(node.data.obId != record.data.obId){
							    		node.set('selectContact', false);	
							    	}
							    }
							);	
           				} else {
           					record.set('selectContact', false);
           				}
           			} else {
           				if(!record.data.selectContact){
           					record.set('selectContact', true);
           					nodeAdd.push(record);
           					view.up('treepanel').store.getRootNode().cascadeBy(
							    function(node){
							    	if(node.data.obId != record.data.obId && node.data.type != 'contact'){
							    		node.set('selectContact', false);	
							    	} 
							    	if(node.data.selectContact == true && node.data.obId != record.data.obId) {
							    		nodeAdd.push(node);
							    	}
							    }
							);	
           				} else {
           					record.set('selectContact', false);
           				}
           			}
           			var target = view.up('tariff-call-report-entity-tree').target;
	           		if(target){
	           			target.store.removeAll();
	           			Ext.iterate(nodeAdd, function(node){
	           				target.store.add({entityPath:CDRUtils.getTreePath(node), type: node.data.type, obId: node.data.obId});	
	           			});
	           		}
	           	}
           },
           
           '#x-cdr-tariff-report-run toolbar button[action=run]' : {
	           	click: function(cmp, e){
	           		var treeType = Ext.getCmp('x-cdr-tariff-report-treetype');
	           		var node = treeType.getSelectionModel().getSelection()[0];
	           		if(!node){
	           			Ext.Msg.alert('Warning', 'Please select the report type');
	           			return;
	           		}
	           		
	           		var entity = Ext.getCmp('x-cdr-tariff-report-condition').down('gridpanel[name=entity]');
	           		if(entity.store.count() <= 0){
	           			Ext.Msg.alert('Warning', 'Please select the report entity');
	           			return;
	           		}
	           		
	           		var sbEntity = [], type;
	           		entity.store.each(function(r){
	           			type = r.data.type;
	           			sbEntity.push(r.data.obId);
	           		});
	           		sbEntity = {type: type, ids: sbEntity};
	           		
	           		var periodObj = Ext.getCmp('x-cdr-tariff-report-condition').down('radiogroup[name=period]').getValue();
	           		var sbPeriod = {type: periodObj.period};
	           		if(periodObj.period == 'preset'){
	           			var container = Ext.getCmp('x-cdr-tariff-report-condition').down('radiogroup[name=period]').down('container[name=presetValue]').down('combo');
	           			sbPeriod.presetValue = container.getValue();
	           			
	           		} else if (periodObj.period == 'last'){
	           			var container = Ext.getCmp('x-cdr-tariff-report-condition').down('radiogroup[name=period]').down('container[name=lastValue]');
	           			sbPeriod.lastValue = container.down('combo').getValue();
	           			sbPeriod.lastTime = container.down('numberfield').getValue();
	           		} else if(periodObj.period == 'from'){
	           			var container = Ext.getCmp('x-cdr-tariff-report-condition').down('radiogroup[name=period]');
	           			if(container){
	           				var fromDate = container.down('datefield[name=fromDate]');
	           				var toDate = container.down('datefield[name=toDate]');
	           				if(fromDate.getValue() == null || toDate.getValue() == null){
	           					Ext.Msg.alert('Warning', 'Please select the from and to Date');
	           					return;
	           				}
	           				sbPeriod.fromDate = Ext.Date.format(fromDate.getValue(), 'd/m/Y');
	           				sbPeriod.toDate =  Ext.Date.format(toDate.getValue(), 'd/m/Y');
	           			}
	           		}
	           		
	           		
	           		//load report
	           		Ext.Ajax.request({
						url: 'runReport.html',
						params: {
							type: node.raw.key,
							entity: JSON.stringify(sbEntity),
							period: JSON.stringify(sbPeriod),
						},
						success: function(response){
							var data = Ext.decode(response.responseText);
							if(data.success) {
								
							}
						},
						failure:function(){
							alert('run report fail...');
						}
					});
	           	}
           },
        });
    }

});